<?php
/**
 * Ticket Model.
 * @package    spf.modules.tickets
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Ticket Model.
 *
 * @package    spf.modules.tickets
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class Project extends SPF_Model {

   /**
    * Constructor.
    *
    * @return  void
    */
   public function __construct() {

      /* construct the parent object */
      parent::__construct('projects');
      
      $this->add_validation('description', true, 'alphanumeric', ' ');
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      parent::__destruct();
   }
   
   public function clear( $keep_id = false ) {
       
       parent::clear($keep_id);
       
       $this->active = 1;
       
   } // clear
   
   public function after_delete( $args ) {
      
      // delete related project records (components, milestones, tickets and comments)
      $sql = "DELETE
                FROM components
               WHERE project_id = ?";
      $this->db->execute($sql, $args);
      
      $sql = "DELETE
                FROM milestones
               WHERE project_id = ?";
      $this->db->execute($sql, $args);
      
      $sql = "DELETE
                FROM comments
               WHERE ticket_id IN (SELECT id FROM tickets WHERE project_id = ?)";
      $this->db->execute($sql, $args);
      
      $sql = "DELETE
                FROM tickets
               WHERE project_id = ?";
      $this->db->execute($sql, $args);
      
      
   } // after_delete
   
   public function components_data() {
   
   	$sql = "SELECT id,
                     description,
                     active
                FROM components
               WHERE project_id = {$this->id}
            ORDER BY description";
      return SPF::$db->get_all($sql);
      
   } // components_data
   
   public function milestones_data() {
   
   	$sql = "SELECT id,
                     description,
                     active
                FROM milestones
               WHERE project_id = {$this->id}
            ORDER BY description";
      return SPF::$db->get_all($sql);
      
   } // milestones_data
   
   public static function browse_data() {
   
   	$sql = "SELECT id,
                     description,
                     active,
                     (SELECT COUNT(*)
                        FROM components
                       WHERE components.project_id = projects.id) AS component_count,
                     (SELECT COUNT(*)
                        FROM milestones
                       WHERE milestones.project_id = projects.id) AS milestone_count,
                     (SELECT COUNT(*)
                        FROM tickets
                       WHERE tickets.project_id = projects.id) AS ticket_count
                  FROM projects
              ORDER BY description";
      return SPF::$db->get_all($sql);
      
   } // browse_data
   
   public static function lookup_options( $project_id, $table_name, $current_id = 0, $inactive = false ) {
      
      $project_id = (int) $project_id;
      $current_id = (int) $current_id;
      
      $sql = "SELECT id, description
                FROM $table_name
               WHERE project_id = $project_id\n";
      
      if( !$inactive )
         $sql .= " AND (active = 1 OR id = {$current_id})\n";
      
      $sql .= ' ORDER BY description';

      $rs =SPF::$db->get_all($sql);
      
      $arr = array();
      foreach( $rs as &$row ) {
         $arr[ $row['id'] ] = $row['description'];
      }
      
      return $arr;
      
   } // lookup_options
   
} // Project

?>
